﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;

namespace wontec_claw
{
    internal class SKCOM
    {
        public Action<List<byte[]>> dataRecv;
        public Action<List<byte>> COM_DataReceived;
        public Action<byte[]> dataSend;
        public Action<SerialPort> setCOM;
        public Action<int> SetBPS;
        SerialPort COM;

        List<byte[]> dataBuff = new List<byte[]>();
        byte[] canData = { 0xAA, 0xCC, 0x43, 0x01, 0x00, 
            0x00, 0x00, 0x00, 0x00,
            0x08,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

        int step = 0;
        int flag = 0;
        int stats = 0;
        int len = 0;
        public int canid = 0;
        public byte[] candata = new byte[12];
        long ticks;
        public void candecode(List<byte> canBuffer)
        {
            if (true)
            {
                if (System.DateTime.Now.Ticks > ticks)
                    step = 0;
                while (canBuffer.Count != 0)
                {
                    ticks = System.DateTime.Now.Ticks + 50 * 10000;
                    if (canBuffer[0] == 0xaa && canBuffer[1] == 0xcc)
                    {

                        var data = new byte[12];
                        for (int i = 0; i < 4; i++)
                            data[i + 8] = canBuffer[-i + 8];
                        for (int i = 0; i < canBuffer[9]; i++)
                            data[i] = canBuffer[i + 10];
                        dataBuff.Add(data);
                        canBuffer.RemoveRange(0, 10 + canBuffer[9]);
                    }
                    else
                        canBuffer.RemoveAt(0);
                }
                //System.Threading.Thread.Sleep(0);
            }
            //return null;
        }

        public SKCOM()
        {
            dataRecv = (List<byte[]> data) => {
                dataBuff = data;
            };
            dataSend = (byte[] data) => {
                var bytes = new byte[canData.Length];
                int offset = 0;
                for (int i = 0; i < canData.Length; i++)
                    bytes[i + offset] = canData[i];

                bytes[offset + 5] = data[11];
                bytes[offset + 6] = data[10];
                bytes[offset + 7] = data[9];
                bytes[offset + 8] = data[8];
                for (int i = 0; i < 8; i++)
                {
                    bytes[offset + 10 + i] = data[i];
                }
                if (data.Length > 12)
                {
                    bytes[3] = (byte)(((data[12] >> 4) & 0xf) != 0 ? 1 : 0);
                    bytes[4] = (byte)((data[12] & 0xf) != 0 ? 1 : 0);
                }
                //bytes[offset + canData.Length - 1] = CRC_8(bytes, offset, canData.Length);
                //offset += canData.Length;
                if (COM == null) return;
                if (!COM.IsOpen) return;
                COM.Write(bytes, 0, bytes.Length);
            };
            setCOM = (SerialPort com) => { COM = com; };
            COM_DataReceived = candecode;
            List<int> canbps = new List<int>(new int[] { 1000, 800, 666, 500, 400, 250, 200, 125, 100, 80, 50, 40, 20, 10 });
            SetBPS = (int value) =>
            {
                if (COM == null) return;
                if (!COM.IsOpen) return;
                //var index = canbps.IndexOf(value);
                //var setbps = new byte[] { 0x55, 0x05, (byte)index, 0xaa, 0x55 };
                //COM.Write(setbps, 0, setbps.Length);
            };


        }
    }
}
